package com.dmap.admin.service.backgroundv1.impl;

import com.dmap.admin.dao.AdminPermissionsMapper;
import com.dmap.admin.dao.AdminRolePermissionsMapper;
import com.dmap.admin.po.AdminPermissions;
import com.dmap.admin.po.AdminPermissionsExample;
import com.dmap.admin.po.AdminRolePermissions;
import com.dmap.admin.po.AdminRolePermissionsExample;
import com.dmap.admin.service.backgroundv1.RolePermissionService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: silent
 * \* Date: 08/12/17 Time: 10:46
 * \* Description:
 * \
 */
@Service
public class RolePermissionServiceImpl implements RolePermissionService {
    @Resource
    private AdminRolePermissionsMapper adminRolePermissionsMapper;
    @Resource
    private AdminPermissionsMapper adminPermissionsMapper;

    @Override
    public AdminRolePermissions insert(Long roleId, Long permissionId) {
        AdminRolePermissions adminRolePermissions = new AdminRolePermissions();
        adminRolePermissions.setRoleId(roleId);
        adminRolePermissions.setPermissionsId(permissionId);
        adminRolePermissions.setCreateTime(System.currentTimeMillis());
        adminRolePermissions.setModifyTime(adminRolePermissions.getCreateTime());
        int success = adminRolePermissionsMapper.insertSelective(adminRolePermissions);
        return (success == 1) ? adminRolePermissions : null;
    }

    @Override
    public boolean isExistInRolePermissionTable(Long roleId, Long permissionId) {
        AdminRolePermissionsExample example = new AdminRolePermissionsExample();
        example.createCriteria().andRoleIdEqualTo(roleId).andPermissionsIdEqualTo(permissionId);
        List<AdminRolePermissions> list = adminRolePermissionsMapper.selectByExample(example);
        return CollectionUtils.isNotEmpty(list);
    }

    @Override
    public List<AdminPermissions> selectPermissionListByRoleId(Long roleId) {
        AdminRolePermissionsExample example = new AdminRolePermissionsExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        List<AdminRolePermissions> adminRolePermissionsList = adminRolePermissionsMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(adminRolePermissionsList)) {
            return null;
        }
        List<Long> permissionIdList = new ArrayList<>();
        for (AdminRolePermissions adminRolePermissions : adminRolePermissionsList) {
            permissionIdList.add(adminRolePermissions.getPermissionsId());
        }
        AdminPermissionsExample adminPermissionsExample = new AdminPermissionsExample();
        adminPermissionsExample.createCriteria().andIdIn(permissionIdList);
        return adminPermissionsMapper.selectByExample(adminPermissionsExample);
    }

    @Override
    public int deletePermissionListByRoleId(Long roleId) {
        AdminRolePermissionsExample example = new AdminRolePermissionsExample();
        example.createCriteria().andRoleIdEqualTo(roleId);
        return adminRolePermissionsMapper.deleteByExample(example);
    }
}